﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using Antlr3.ST;
using System.CodeDom;
using System.Reflection;
using System.CodeDom.Compiler;
using System.Collections.Generic;

namespace Dynamo.Core
{
	public class DynamicBuilder
	{
		private Assembly aDynamicAssembly;

		private CodeCompileUnit ccuCompileUnit;
		private CodeDomProvider cdpCodeDomProvider;
		private CompilerParameters cpCompilerParameters;
		private CodeNamespace cnTransferObjectNamespace;

		public DynamicBuilder()
		{
			cdpCodeDomProvider = new Microsoft.CSharp.CSharpCodeProvider();

			cpCompilerParameters = new CompilerParameters();

			cpCompilerParameters.GenerateExecutable = false;
			cpCompilerParameters.GenerateInMemory = false;
			cpCompilerParameters.IncludeDebugInformation = false;

			cpCompilerParameters.ReferencedAssemblies.Add("System.dll");

			ccuCompileUnit = new CodeCompileUnit();

			// Declare a new namespace 
			cnTransferObjectNamespace = new CodeNamespace("Dynamo.GeneratedCode");
			// Add the new namespace to the compile unit.
			ccuCompileUnit.Namespaces.Add(cnTransferObjectNamespace);

			// Add the new namespace using for the required namespaces.
			cnTransferObjectNamespace.Imports.Add(new CodeNamespaceImport("System"));
			cnTransferObjectNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
		}

		public object Build(Assembly[] aaAssembly, string sTemplate, Dictionary<string, object> dValues)
		{
			StringTemplate query = new StringTemplate(sTemplate);

			foreach (KeyValuePair<string, object> item in dValues)
			{
				query.SetAttribute(item.Key, item.Value);
			}

			foreach (Assembly aAssembly in aaAssembly)
			{
				cpCompilerParameters.ReferencedAssemblies.Add(aAssembly.ManifestModule.FullyQualifiedName);
			}

			CompilerResults cr = cdpCodeDomProvider.CompileAssemblyFromSource(cpCompilerParameters, query.ToString());

			aDynamicAssembly = cr.CompiledAssembly;

			return aDynamicAssembly.CreateInstance((string)dValues["class"]);
		}

		public string Namespace
		{
			get
			{
				return cnTransferObjectNamespace.Name;
			}
		}

		public Assembly DynamicAssembly
		{
			get
			{
				return aDynamicAssembly;
			}
			set
			{
				aDynamicAssembly = value;
			}
		}

		public void AddType(CodeTypeDeclaration ctdType)
		{
			cnTransferObjectNamespace.Types.Add(ctdType);
		}

		public void Compile()
		{
			//CompilerResults results = cdpCodeDomProvider.CompileAssemblyFromDom(cpCompilerParameters, ccuCompileUnit);
			CompilerResults crCompilerResults = cdpCodeDomProvider.CompileAssemblyFromSource(cpCompilerParameters, "");

			if (crCompilerResults.Errors.Count > 0)
			{
				CompilerErrorCollection errors = crCompilerResults.Errors;
			}
			else
			{
				aDynamicAssembly = crCompilerResults.CompiledAssembly;
			}
		}
	}
}
